/*******************************************************************************
 * Copyright (c) 2003 Boris Pruessmann and others. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Common Public License v0.5 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui.internal;

import java.util.Map;

import net.sourceforge.perforce.api.CharSetEnum;
import net.sourceforge.perforce.core.api.IPerforceServer;

/**
 * Class used to store detail information about a specific Perforce server.
 * 
 * @version $Revision: 488 $
 * @author <a href="mailto:boris@pruessmann.org">Boris Pruessmann</a>
 */
public class ServerDetail {
  // ---------------------------------------------------------------------------
  // Private Variables

  /** Stores the server address. */
  private String serverAddress = "";
  /** Stores the server port. */
  private int serverPort;
  /** Stores the client name. */
  private String client = "";
  /** Stores the host name. */
  private String host = "";
  /** Stores the user name. */
  private String user = "";
  /** Stores the password. */
  private String password = "";
  /** Stores the character set. */
  private CharSetEnum characterSet = CharSetEnum.NOCONV;
  /** Stores the background updates flag. */
  private boolean backgroundUpdates;

  // -------------------------------------------------------------------------
  // Public Constructors

  /**
   * Constructor for ServerDetail.
   * 
   * @param properties map containing detail information.
   */
  public ServerDetail(Map<String, ?> properties) {
    String tempString = (String) properties.get("port"); //$NON-NLS-1$
    if (tempString != null) {
      int pos = tempString.lastIndexOf(':');
      if (pos > 0) {
        serverAddress = tempString.substring(0, pos);
      } else {
        serverAddress = "localhost";
      }
      serverPort = Integer.parseInt(tempString.substring(pos + 1));
    }

    tempString = (String) properties.get("client"); //$NON-NLS-1$
    if (tempString != null) {
      client = tempString;
    }

    tempString = (String) properties.get("host"); //$NON-NLS-1$
    if (tempString != null) {
      host = tempString;
    }

    tempString = (String) properties.get("user"); //$NON-NLS-1$
    if (tempString != null) {
      user = tempString;
    }

    tempString = (String) properties.get("password"); //$NON-NLS-1$
    if (tempString != null) {
      password = tempString;
    }

    tempString = (String) properties.get("charset"); //$NON-NLS-1$
    if (tempString != null) {
      characterSet = CharSetEnum.fromString(tempString);
    }

    Boolean tempBoolean = (Boolean) properties.get("backgroundUpdates"); //$NON-NLS-1$
    if (tempBoolean != null) {
      backgroundUpdates = tempBoolean.booleanValue();
    }
  }

  /**
   * Constructor for ServerDetail.
   * 
   * @param theServerAddress the address of the server.
   * @param theServerPort the server port.
   * @param theClient the client name.
   * @param theHost the host name.
   * @param theUser the user name.
   * @param thePassword the password.
   * @param theCharacterSet the character set.
   * @param performBackgroundUpdates indicator for background updates.
   */
  public ServerDetail(String theServerAddress, int theServerPort, String theClient, String theHost,
      String theUser, String thePassword, CharSetEnum theCharacterSet,
      boolean performBackgroundUpdates) {
    serverAddress = (theServerAddress != null) ? theServerAddress : ""; //$NON-NLS-1$
    serverPort = theServerPort;
    client = (theClient != null) ? theClient : ""; //$NON-NLS-1$
    host = (theHost != null) ? theHost : ""; //$NON-NLS-1$
    user = (theUser != null) ? theUser : ""; //$NON-NLS-1$
    password = (thePassword != null) ? thePassword : ""; //$NON-NLS-1$
    characterSet = theCharacterSet;
    backgroundUpdates = performBackgroundUpdates;
  }

  /**
   * Constructor for ServerDetail.
   * 
   * @param server the perforce server.
   */
  public ServerDetail(IPerforceServer server) {
    final String tempString = server.getPort();
    int pos = tempString.lastIndexOf(':');
    serverAddress = tempString.substring(0, pos);
    serverPort = Integer.parseInt(tempString.substring(pos + 1));

    client = server.getClient();
    host = server.getHost();
    user = server.getUser();
    password = server.getPassword();
    characterSet = server.getCharset();
    backgroundUpdates = server.shouldIncludeInBackgroundUpdates();
  }

  //----------------------------------------------------------------------------
  // -- Public Methods

  /**
   * Called to retrieve the network address of the server.
   * 
   * @return the server's address.
   */
  public String getServerAddress() {
    return serverAddress;
  }

  /**
   * Called to retrieve the server port.
   * 
   * @return the server port.
   */
  public int getServerPort() {
    return serverPort;
  }

  /**
   * Called to retrieve the client name.
   * 
   * @return the client name.
   */
  public String getClient() {
    return client;
  }

  /**
   * Called to retrieve the host name.
   * 
   * @return the host name.
   */
  public String getHost() {
    return host;
  }

  /**
   * Called to retrieve the user name.
   * 
   * @return the user name.
   */
  public String getUser() {
    return user;
  }

  /**
   * Call this method to retrieve the password.
   * 
   * @return the password.
   */
  public String getPassword() {
    return password;
  }

  /**
   * Called to retrieve the character set.
   * 
   * @return the character set.
   */
  public CharSetEnum getCharacterSet() {
    return characterSet;
  }

  /**
   * Called to check the state of backgronud updates.
   * 
   * @return <code>true</code> if background updates will be performed.
   */
  public boolean getBackgroundUpdates() {
    return backgroundUpdates;
  }
}
